package application.openGL;

import org.lwjgl.opengl.*;
import org.lwjgl.*;

import application.Application;

public abstract class GLApplication extends Application
{
    private int refreshRate = Math.min(getScreenRefreshRate(), 60);
    @Override
    protected void initializeApplication() 
    {
	try
	{
	    Display.setDisplayMode(new DisplayMode(size.width, size.height));
	    Display.setTitle(title);
	    Display.create();
	}
	catch(LWJGLException e)
	{
	    e.printStackTrace();
	}
	initOpenGL();
    }
    protected abstract void initOpenGL();
    @Override
    protected void run() 
    {
	init();
	while(!Display.isCloseRequested())
	{
	    redraw();
	    update(getDelta());
	}
	Display.destroy();
	System.exit(0);
    }
    @Override
    protected void redraw() 
    {
	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	render();
	Display.update();
	Display.sync(refreshRate);
    }
    private long prevTime = System.nanoTime();
    private double getDelta()
    {
	long now = System.nanoTime();
	long difference = now-prevTime;
	prevTime = now;
	return ((double)difference)/(1000000000.0d/(double)refreshRate);
    }
    public void enableAlphaBlending()
    {
	GL11.glBlendFunc (GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    }
    public void enableAdditiveBlending()
    {
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
    }
}